#include <public/humsat_unitime.h>
#include <public/comm_prog.h>
#include <iostream>
using namespace std;

HUMSATCommProg_t CHUMSATCommManager::commProgTable[MAX_HUMSATCommProgs];

void CHUMSATCommManager::ClearHUMSATCommProgTable( ){
	// This function clear the the communication program table, writting 0 for
	//every init_time and end_time of the elements of the table
	CUniTime tiempo;
	tiempo.BuildUniTime(0,0,0,0,0);
	tiempo.GetUniTimeSeconds();

	for (int x=0; x<=MAX_HUMSATCommProgs ;x++){
		commProgTable[x].init_time=tiempo;
		commProgTable[x].end_time=tiempo;
		commProgTable[x].txrxConfig=00000001;
	}
}




void CHUMSATCommManager::InitHUMSATCommProgTable( ){
	//This function initializes the configuration of the communication windows
	//with sensors(SSI) and station (SGI).
	//utilizar las funciones unitime para pasar el tiempo a segundos
	//commProgTable[0].init_time=BuildUniTime(0,0,0,0);
	int y=0;
	int z=1;
	int anterior=1;
	for (int x=0; x<=MAX_HUMSATCommProgs ;x++){
		CUniTime tiempoInicio;
		CUniTime tiempoFin;
		tiempoInicio.BuildUniTime(15,0,0,0,y+z);
		y=y+10;
		z=0;
		tiempoFin.BuildUniTime(15,0,0,0,y);
		y=y+5;
		tiempoInicio.GetUniTimeSeconds();
		tiempoFin.GetUniTimeSeconds();
		commProgTable[x].init_time=tiempoInicio;
		commProgTable[x].end_time=tiempoFin;
		if (anterior==1){
			commProgTable[x].txrxConfig=00000000;
			anterior=0;//1 SGI
		}else if(anterior==0){
			commProgTable[x].txrxConfig=00000001;
			anterior=1;
		} //0 SSI
	}
}



bool_t CHUMSATCommManager::IsHUMSATCommValid( HUMSATCommProg_t &commProg){
// This function check if a commProg is Valid. A CommProg is valid if
//end_time.GetUniTimeSeconds() is >  CUniTime::GetCurrentUniTimeSeconds()
// and init_time.GetUniTimeSeconds() < end_time.GetUniTimeSeconds();
	if ((commProg.end_time.GetUniTimeSeconds()) >= (CUniTime::GetCurrentUniTimeSeconds()) and (commProg.init_time.GetUniTimeSeconds()) <= (commProg.end_time.GetUniTimeSeconds())){
		return true;
	}else
		return false;

}


bool_t CHUMSATCommManager::GetNextHUMSATCommProg( HUMSATCommProg_t &commProg){
	// This function retrieves the next Valid HUMSATCommProg
	// return true if a valid CommProg has been retrieved, false if not

	//commProg.init_time=time.BuildUniTime(15,0,0,0,0).GetUniTimeSeconds();
	//commProg.end_time=time.GetCurrentUniTimeSeconds();
	//if (IsHUMSATCommValid(commProg)){

	for (int x=0; x<=MAX_HUMSATCommProgs-1 ;x++){
		if((commProgTable[x].init_time.GetUniTimeSeconds())<=(commProgTable[x+1].init_time.GetUniTimeSeconds())){
			if (IsHUMSATCommValid(commProgTable[x])){
				commProg=commProgTable[x];
				return 0;
			}
		}
	}
};


bool_t CHUMSATCommManager::ProgramHUMSATComm( HUMSATCommProg_t &commProg){
//This function look for a not valid program in the commProgTABLE and
//write the commProg in it
//param commProg program to write
//return true if a valid CommProg has been written,
	//añadir un programa a la tabla, buscando en ella un programa no válido
    //y utilizando su hueco
	int y=0;
	if (!IsHUMSATCommValid(commProg)){
		 CUniTime tiempo;
		 tiempo.BuildUniTime(15,0,0,4,0+y);
	}


};


